package com.quanyan.runningriding.web.running;


import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.validation.base.Insert;
import com.quanyan.runningriding.entity.vo.run.req.AnnualMileageVo;
import com.quanyan.runningriding.entity.vo.run.req.RunRecordVo;
import com.quanyan.runningriding.entity.vo.run.req.RunRecordVos;
import com.quanyan.runningriding.entity.vo.run.resp.*;
import com.quanyan.runningriding.service.RunRecordService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * RunRecordController
 *
 * @author Tu Chun
 * @date 2016/8/23
 */
@RestController
@RequestMapping(value = "/api/run")
public class RunRecordController {
    @Autowired
    private RunRecordService runRecordService;
    
    private static final Logger logger = LoggerFactory.getLogger(RunRecordController.class);

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public APIResponse<?> submit(@RequestBody @Validated(Insert.class) RunRecordVo runRecordVo){
        Integer result=runRecordService.insertRunRecord(runRecordVo);
        if(result==-2){
            return APIResponse.returnFail("用户不存在，请重新登录");
        }
        if(result==-1){
            return APIResponse.returnFail("更新用户等级失败");
        }
        if(result>0){
           return APIResponse.returnSuccess(result);
        }else{
           return APIResponse.returnFail("上传运动记录异常");
        }
    }
    @RequestMapping(value = "/monthMileage",method = RequestMethod.POST)
    public APIResponse<MonthMileageVo> monthMileage(@RequestBody Map<String,Object> paramMap){
        String userId=MapUtils.getString(paramMap,"userId");
        if(userId==null || "".equals(userId)){
            return APIResponse.returnFail("用户id不能为空");
        }
        APIResponse<MonthMileageVo>  result=runRecordService.getMonthMileage(userId);
        return result;
    }
    @RequestMapping(value = "/totalMileage",method = RequestMethod.POST)
    public APIResponse<TotalMileage> totalMileage(@RequestBody Map<String,Object> paramMap){
        String userId=MapUtils.getString(paramMap,"userId");
        if(userId==null || "".equals(userId)){
            return APIResponse.returnFail("用户id不能为空");
        }
        APIResponse<TotalMileage>  result=runRecordService.getTotalMileage(userId);
        return result;
    }
    @RequestMapping(value = "/annualMileage",method = RequestMethod.POST)
    public APIResponse<PageObj<AnnualMileage>> annualMileage(@RequestBody @Valid AnnualMileageVo annual){
        PageObj<AnnualMileage>  result=runRecordService.getAnnualMileage(annual);
        return APIResponse.returnSuccess(result);
    }
    @RequestMapping(value = "/trackDisplay",method = RequestMethod.POST)
    public APIResponse<RunTrackDetail> trackDisplay(@RequestBody Map<String,Object> paramMap){
        Integer runRecordId=MapUtils.getInteger(paramMap,"runRecordId");
        if(runRecordId==null||runRecordId<=0){
            return APIResponse.returnFail("运动记录id参数错误");
        }
        APIResponse<RunTrackDetail>  result=runRecordService.trackDisplay(runRecordId);
        return result;
    }
    @RequestMapping(value = "/runRecordDetails",method = RequestMethod.POST)
    public APIResponse<RunRecordDetail> runRecordDetails(@RequestBody Map<String,Object> paramMap){
        Integer runRecordId=MapUtils.getInteger(paramMap,"runRecordId");
        if(runRecordId==null||runRecordId<=0){
            return APIResponse.returnFail("运动记录id参数错误");
        }
        APIResponse<RunRecordDetail>  result=runRecordService.runRecordDetails(runRecordId);
        return result;
    }
    @RequestMapping(value = "/chartDetails",method = RequestMethod.POST)
    public APIResponse<ChartDetail> chartDetails(@RequestBody Map<String,Object> paramMap){
        Integer runRecordId=MapUtils.getInteger(paramMap,"runRecordId");
        if(runRecordId==null||runRecordId<=0){
            return APIResponse.returnFail("运动记录id参数错误");
        }
        APIResponse<ChartDetail>  result=runRecordService.chartDetails(runRecordId);
        return result;
    }
    @RequestMapping(value = "/batchSynchroniseRunRecord", method = RequestMethod.POST)
    public APIResponse<?> batchSynchroniseRunRecord(@RequestBody @Validated(Insert.class)RunRecordVos runRecordVos) {
        APIResponse<?> result = runRecordService.batchSynchroniseRunRecord(runRecordVos.getRunRecordVos());
        return result;
    }
}
